package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.MyRewardAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.InviteRewardOpenBean;
import com.yiwei.jmx.api.bean.PrizeListBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.view.OnLoadeMoreListener;
import com.yiwei.jmx.view.RedRewardDialog;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyRewardMoreActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private String status;
    private Unbinder unbiner;
    private MyRewardAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getPrizeList(page);
        }
    };
    private RedRewardDialog openWardDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_reward_more);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("更多奖励记录");
        openWardDialog = new RedRewardDialog(this);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getPrizeList(1);
            }
        });
        adapter = new MyRewardAdapter(this);
        adapter.setOnChooseListener(new MyRewardAdapter.OnChooseListener() {
            @Override
            public void onSuccess(String id) {
                getPrizeTake(id);
            }
        });
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);
        getPrizeList(1);
    }

    public void getPrizeList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        PrizeListBean bean = new PrizeListBean();
        bean.page = page;
        Api.api_service.getInviteRewardList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.data, page == 1);
                });
    }

    public void getPrizeTake(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        InviteRewardOpenBean bean = new InviteRewardOpenBean();
        bean.keyword = id;
        Api.api_service.getInviteRewardOpen(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    openWardDialog.setData(result.data.money);
                    openWardDialog.show();
                    onLoadeMoreListener.setPage();
                    getPrizeList(1);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                getPrizeTake("ALL");
                break;
        }
    }
}
